package com.asset.modules.common.handler;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.asset.modules.common.model.AmountBean;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author chenchen
 * @date  2020-10-06
 *
 * 固资统计信息拦截器,在数据输出尾部追加合计
 */
public class AssetAmountHandler extends AbstractCellWriteHandler {

    private static final Logger  LOGGER = LoggerFactory.getLogger(AssetAmountHandler.class);


    private AmountBean ab;

    public AssetAmountHandler(AmountBean bean){
        this.ab = bean;
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList,
                                 Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

        LOGGER.info("第{}行，第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex());
        if((cell.getRowIndex() == ab.getTotalRecords()+4)&&cell.getColumnIndex()==17){
            Row row = writeSheetHolder.getCachedSheet().createRow(cell.getRowIndex()+1);
            row.setHeight((short) 600);
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            CellStyle cs = workbook.createCellStyle();

            cs.setAlignment(HorizontalAlignment.CENTER);
            cs.setVerticalAlignment(VerticalAlignment.CENTER);
            cs.setBorderBottom(BorderStyle.THIN);
            cs.setBorderLeft(BorderStyle.THIN);
            cs.setBorderRight(BorderStyle.THIN);
            cs.setBorderTop(BorderStyle.THIN);
            for(int i=0; i<=17; i++){
                Cell tmpCell = row.createCell(i);
                switch (i){
                    //合计标题
                    case 0:
                        tmpCell.setCellValue("合计");
                        break;
                    //合计账面总量
                    case 5:
                        tmpCell.setCellValue(ab.getTotalRegNum());
                        break;
                    //合计账面金额
                    case 6:
                        tmpCell.setCellValue(ab.getTotalRegMoney());
                        break;
                    //合计盘点数量
                    case 7:
                        tmpCell.setCellValue(ab.getTotalCheckNum());
                        break;
                    //合计盘点金额
                    case 8:
                        tmpCell.setCellValue(ab.getTotalCheckedMoney());
                        break;
                    //合计盈亏数量
                    case 9:
                        tmpCell.setCellValue(ab.getTotalEarnNum());
                        break;
                    //合计盈亏金额
                    case 10:
                        tmpCell.setCellValue(ab.getTotalEarnMoney());
                        break;
                    //合计总净值
                    case 11:
                        tmpCell.setCellValue(ab.getTotalValue());
                        break;
                    default:
                        break;
                }
                tmpCell.setCellStyle(cs);
            }
        }
    }
}
